package com.qyhl.module_practice.substreet.fragment_new;


import com.qyhl.module_practice.common.PracticeUrl;
import com.qyhl.webtv.basiclib.utils.StringUtils;
import com.qyhl.webtv.basiclib.utils.network.EasyHttp;
import com.qyhl.webtv.basiclib.utils.network.callback.SimpleCallBack;
import com.qyhl.webtv.basiclib.utils.network.exception.ApiException;
import com.qyhl.webtv.commonlib.common.CommonUtils;
import com.qyhl.webtv.commonlib.entity.civilized.PracticeListBean;
import com.qyhl.webtv.commonlib.entity.civilized.PracticeSearchBean;

import java.util.List;

public class SubStreetNewModel implements SubStreetNewContract.SubStreetNewModel {
    private SubStreetNewPresenter mPresenter;

    SubStreetNewModel(SubStreetNewPresenter mPresenter) {
        this.mPresenter = mPresenter;
    }

    @Override
    public void getList(String id, String type, final String page) {
        String url;
        if (StringUtils.isEmpty(type)) {
            url = PracticeUrl.GET_PRACTICE_STREET_MAP_LIST;
        } else if (type.equals("0")) {
            url = PracticeUrl.GET_PRACTICE_PLACE_LIST;
        } else if(type.equals("3")){
             url =PracticeUrl.GET_PRACTICE_INS_LIST;
        }else{
            if(type.equals("1")){
                type = "1";
            }else{
                type = "2";
            }
            url = PracticeUrl.GET_STREET_LIST;
        }

        EasyHttp.get(url)
                .params("siteId", CommonUtils.getInstance().getSite() + "")
                .params("instId", id)
                .params("type", type)
                .params("page", page)
                .execute(new SimpleCallBack<List<PracticeListBean>>() {
                    @Override
                    public void onError(ApiException e) {
                        if (e.getCode() == 202) {
                            mPresenter.setError("暂无任何内容！", !page.equals("1"));
                        } else {
                            mPresenter.setError("获取详情失败，请重新尝试！", !page.equals("1"));
                        }
                    }

                    @Override
                    public void onSuccess(List<PracticeListBean> practiceListBeans) {
                        if (practiceListBeans != null && practiceListBeans.size() > 0) {
                            mPresenter.setList(practiceListBeans, !page.equals("1"));
                        } else {
                            mPresenter.setError("暂无任何内容！", !page.equals("1"));
                        }
                    }
                });
        
    }

    @Override
    public void doSearch(String key, String type) {
        
        EasyHttp.get(PracticeUrl.GET_PRACTICE_SEARCH)
                .params("key", key)
                .params("siteId", CommonUtils.getInstance().getSite() + "")
                .params("type", type)
                .execute(new SimpleCallBack<PracticeSearchBean>() {
                    @Override
                    public void onError(ApiException e) {
                        mPresenter.setError("网络异常，搜索失败！", false);
                    }

                    @Override
                    public void onSuccess(PracticeSearchBean practiceSearchBean) {
                        if (practiceSearchBean.getInst() != null && practiceSearchBean.getInst().size() > 0) {
                            mPresenter.setSearch(practiceSearchBean.getInst());
                        } else {
                            mPresenter.setError("暂无任何相关内容！", false);
                        }
                    }
                });
                
    }
}
